// 表单接口

package api

import (
	"api/common/logger"
	"api/common/orm"
	"api/common/web"
	"net/http"
)

// @TagName 表单接口
// @Controller FormController
type FormController struct{}

// @Title 开设专业列表数据
// @Route /form/specialList --> SpecialList
// @Method post
func (this *FormController) SpecialList(w http.ResponseWriter, r *http.Request) {
	defer web.PanicHandler(w, r)

	count := 0
	sql := "select count(1) from basic_special"
	conn := orm.NewOrm()
	err := conn.Raw(sql).QueryRow(&count)
	if err != nil {
		logger.Error("查询记录数量发生错误", err)
		web.ServeJSON(w, web.JsonMsg{false, "查询记录数量发生错误" + err.Error(), 1})
		return
	}

	data := []map[string]interface{}{}
	sql = "select id, pid, `name`, types, quota," +
		" (select count(1) from student_info where s.pid>0 and `exit`=false" +
		" and special_id=s.id and (`status`=1 or `status`=2 or `status`=5)) quota2" +
		" from basic_special s order by pid, sequ desc, id"
	data, err = conn.Raw(sql).QueryMaps()
	if err != nil {
		logger.Error("查询记录发生错误", err)
		web.ServeJSON(w, web.JsonMsg{false, "查询记录发生错误" + err.Error(), 1})
		return
	}

	result := map[string]interface{}{}
	result["status"] = 0
	result["success"] = true
	result["msg"] = "查询成功"
	result["root"] = data
	result["total"] = count
	web.ServeJSON(w, result)
}

// @Title 是否显示“免冠证件照”上传按钮
// @Route /form/showPictureUpload --> ShowPictureUpload
// @Method post
func (this *FormController) ShowPictureUpload(w http.ResponseWriter, r *http.Request) {
	defer web.PanicHandler(w, r)

	value := ""
	sql := "select `value` from basic_global_set where `name` = ?"
	conn := orm.NewOrm()
	err := conn.Raw(sql, "showPictureUpload").QueryRow(&value)
	if err != nil {
		logger.Error(err)
		return
	}

	showPictureUpload := true
	if value != "true" {
		showPictureUpload = false
	}

	data := map[string]interface{}{}
	data["status"] = 0
	data["success"] = true
	data["msg"] = "获取显示状态成功"
	data["showPictureUpload"] = showPictureUpload

	web.ServeJSON(w, data)
}
